<template>
    <div ref="chartBarRef" class="chart-box bg-white pd-16">
        <div class="chart-box">
            <div class="flex flex-jc-sb">
                <div class="fs-14 bold">{{ title }}</div>
                <div class="flex fs-12">
                    <div class="icon blue mg-r-10"></div>
                    <div>人数</div>
                </div>
            </div>
            <div class="chart-container" ref="chartRef">
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref, watch, onMounted ,onActivated} from 'vue'
import * as echarts from 'echarts'
import { getOpenSevenDays, getWesureInsureSevenDays } from '@/api/dashboard/weinsure.ts'
const props = defineProps({
    title: {
        type: String,
        default: '近七日加减保趋势'
    },
    insureType: {
        type: String,
        default: 'dashboardDay'
    },
    queryData: {
        type: Object,
        default() {
            return {}
        }
    }
})

const chartRef = ref(null)
const chartBarRef = ref(null)
const myChart = ref(null)

const createChartOption = (date, data) => {
    const chartOption =
    {
        tooltip: {
        },
        xAxis: {
            type: 'category',
            data: date,
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#EAEAEF',
                    type: 'solid'
                },
            },
            axisLabel: {
                show: true,
                color: '#9497B1',
                margin: 20
            }
        },
        yAxis: {
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            },
            axisLabel: {
                show: true,
                color: '#9497B1'
            }
        },
        grid: {
            top: 40,
            bottom: 60,
            left: 40,
            right: 20,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow',
                }
            }
        },
        series: [{
            name: '人数',
            type: 'bar',
            data: data,
            seriesLayoutBy: 'row',
            barWidth: '20px',
            color:
                new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                        offset: 0,
                        color: '#1A64FD'
                    },
                    {
                        offset: 1,
                        color: '#657EFB'
                    }
                ])
        }]
    }
    return chartOption
}


const setOption = (date, data) => {
    myChart.value.setOption(createChartOption(date, data))
}

const resize = () => {
    myChart.value.resize()
}

const getData = () => {
    const query = {
        ...props.queryData
    }
    if (props.insureType === 'dashboardGroup') {
        query.insureCompanyType = 'insure_type_tyrb'
    }
  getWesureInsureSevenDays(query).then(res => {
      console.log(res,123434646)
      // setOption(res.date, res.openCount)
      setOption(res.date, res.insureCount)
    })
}

defineExpose({
    chartBarRef,
    resize
})

watch(() => props, (newval) => {
    getData()
}, {
    deep: true,
    immediate: true
})

onMounted(() => {
    myChart.value = echarts.init(chartRef.value)
})
onActivated(() => {
    myChart.value = echarts.init(chartRef.value)
})
</script>
<style lang="scss" scoped>
.chart-box {
    box-sizing: border-box;
}

.chart-box,
.chart-container {
    width: 100%;
    height: 100%;
}

.icon {
    width: 12px;
    height: 12px;
    border-radius: 2px;
}

.blue {
    background: #2567FC;
}

.light-blue {
    background: #EBEFFE;
}
</style>
